home *** CD-ROM | disk | FTP | other *** search
Wrap
/* * SFtoSpr - Star Fighter 3000 graphics converter * Directory savebox * Copyright (C) 2000 Chris Bazley * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public Licence as published by * the Free Software Foundation; either version 2 of the Licence, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public Licence for more details. * * You should have received a copy of the GNU General Public Licence * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ /* ANSI library files */ #include <stdlib.h> #include <stdio.h> #include <string.h> #include <stdbool.h> /* RISC OS library files */ #include "wimp.h" #include "toolbox.h" #include "event.h" #include "wimplib.h" #include "saveas.h" #include "window.h" #include "gadgets.h" #include "flex.h" /* My library files */ #include "err.h" #include "msgtrans.h" #include "hourglass.h" #include "Loader.h" #include "Macros.h" #include "ViewsMenu.h" #include "TboxBugs.h" /* Local headers */ #include "Scan.h" #include "SaveDir.h" #include "Utils.h" #include "Main.h" /* Gadget IDs */ #define SAVEDIR_RADIO_TOSF3000 0x01 #define SAVEDIR_RADIO_TOSPRITES 0x0c typedef struct _SaveDirData { ObjectId saveas_id; ObjectId window_id; char *input_path; #ifndef SAVEAS_CRAP char *reset_file_name; #endif ComponentId reset_direction; } SaveDirData; /* ----------------------------------------------------------------------- */ /* Function prototypes */ static ToolboxEventHandler _SaveDir_deletedhandler, _SaveDir_savehandler, _SaveDir_buttonshandler; /* ----------------------------------------------------------------------- */ /* Public functions */ ObjectId SaveDir_create(char *inputpath) { SaveDirData *newblk; /* Grab memory */ newblk = malloc(sizeof(SaveDirData)); if(newblk == NULL) MG_RETV("NoMem", NULL_ObjectId) /* failure */ newblk->input_path = copystring(inputpath); if(newblk->input_path == NULL) goto errexit1; #ifndef SAVEAS_CRAP newblk->reset_file_name = NULL; #endif /* Create object */ if(E(toolbox_create_object(0, "SaveDir", &newblk->saveas_id))) goto errexit2; if(E(saveas_get_window_id(0, newblk->saveas_id, &newblk->window_id))) goto errexit3; /* Store default settings */ #ifndef SAVEAS_CRAP newblk->reset_file_name = copystring(inputpath); if(newblk->reset_file_name == NULL) goto errexit3; #endif if(E(radiobutton_get_state(0, newblk->window_id, SAVEDIR_RADIO_TOSF3000, NULL, &newblk->reset_direction))) goto errexit3; /* Add entry to iconbar menu */ if(E(ViewsMenu_add(newblk->saveas_id, msgs_lookup_sub1("DirDialogueList", tail(inputpath, 3)), inputpath))) goto errexit3; /* Set up window */ if(E(saveas_set_file_name(0, newblk->saveas_id, inputpath)) || E(saveas_set_file_type(0, newblk->saveas_id, FILETYPE_DIR))) goto errexit4; /* Register Toolbox event handlers Note that ObjectDeleted handler is registered AFTER anything that could cause an error and therefore premature deletion! */ if(!E(event_register_toolbox_handler(newblk->saveas_id, SaveAs_SaveToFile, _SaveDir_savehandler, newblk)) && !E(event_register_toolbox_handler(newblk->saveas_id, SaveAs_DialogueCompleted, delete_object_handler, newblk)) && !E(event_register_toolbox_handler(newblk->window_id, ActionButton_Selected, _SaveDir_buttonshandler, newblk)) && !E(event_register_toolbox_handler(newblk->saveas_id, Toolbox_ObjectDeleted, _SaveDir_deletedhandler, newblk))) return newblk->saveas_id; /* success */ errexit4: RE(ViewsMenu_remove(newblk->saveas_id)); errexit3: RE(toolbox_delete_object(0, newblk->saveas_id)); errexit2: #ifndef SAVEAS_CRAP free(newblk->reset_file_name); #endif free(newblk->input_path); errexit1: free(newblk); return NULL_ObjectId; /* failure */ } /* ----------------------------------------------------------------------- */ /* Private functions */ /* * Toolbox event handlers */ static int _SaveDir_buttonshandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle) { ActionButtonSelectedEvent *abse = (ActionButtonSelectedEvent *)event; SaveDirData *blk = (SaveDirData *)handle; if(!FLAG_SET(abse->hdr.flags, ActionButton_Selected_Adjust)) return 0; /* not interested */ switch(id_block->self_component) { case 0x82bc02: /* Cancel button */ /* Reset dbox state */ #ifndef SAVEAS_CRAP if(blk->reset_file_name != NULL) RE(saveas_set_file_name(0, blk->saveas_id, blk->reset_file_name)); #else RE(saveas_set_file_name(0, blk->saveas_id, blk->input_path)); #endif RE(radiobutton_set_state(0, id_block->self_id, blk->reset_direction, 1)); break; #ifndef SAVEAS_CRAP /* no point saving state if dbox closes anyway */ case 0x82bc03: /* Save button */ /* Additional dbox state saving (radio buttons are read on save) */ int len; E_RETV(saveas_get_file_name(0, blk->saveas_id, NULL, 0, &len), 1) free(blk->reset_file_name); blk->reset_file_name = malloc(len+1); if(blk->reset_file_name == NULL) err_complain(255, msgs_global("NoMem")); else RE(saveas_get_file_name(0, blk->saveas_id, blk->reset_file_name, len, NULL)) break; #endif default: return 0; /* unknown component */ } return 1; /* claim event */ } /* ----------------------------------------------------------------------- */ static int _SaveDir_savehandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle) { SaveAsSaveToFileEvent *save_to_file_block = (SaveAsSaveToFileEvent *)event; SaveDirData *blk = (SaveDirData *)handle; unsigned int flags = 0; /* For the moment we just create the root directory */ if(!ViewsMenu_strcmp_nc(save_to_file_block->filename, "<Wimp$Scrap>")) { _kernel_osfile_block kosfb; kosfb.start = 0; /* default number of entries */ if(_kernel_osfile(8, save_to_file_block->filename, &kosfb) == _kernel_ERROR) { _kernel_oserror *e = _kernel_last_oserror(); err_report(e->errnum, msgs_lookup_sub1("DirFail", e->errmess)); } else flags = 1; /* success */ } else M("NoDirtoApp"); if(flags == 1) { /* N.B. Bizarrely, the underlying window may have been deleted by the time we receive a SaveAs_SaveCompleted event - so we must do our business here. */ /* Read conversion operation from radio buttons */ if(E(radiobutton_get_state(0, blk->window_id, SAVEDIR_RADIO_TOSF3000, NULL, &blk->reset_direction))) goto exit; /* Create new Scan object */ char *buf; if(E(loader_canonicalise(&buf, NULL, NULL, save_to_file_block->filename))) goto exit; ObjectId batchid = Scan_create(blk->input_path, buf, blk->reset_direction == SAVEDIR_RADIO_TOSF3000); free(buf); if(batchid != NULL_ObjectId) RE(toolbox_show_object(0, batchid, Toolbox_ShowObject_Centre, 0, 0, 0)); } exit: saveas_file_save_completed(flags, id_block->self_id, save_to_file_block->filename); return 1; /* claim event */ } /* ----------------------------------------------------------------------- */ static int _SaveDir_deletedhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle) { SaveDirData *blk = (SaveDirData *)handle; /* deregister handlers */ RE(event_deregister_toolbox_handler(id_block->self_id, SaveAs_SaveToFile, _SaveDir_savehandler, handle)); RE(event_deregister_toolbox_handler(id_block->self_id, SaveAs_DialogueCompleted, delete_object_handler, handle)); RE(event_deregister_toolbox_handler(blk->window_id, ActionButton_Selected, _SaveDir_buttonshandler, handle)); RE(event_deregister_toolbox_handler(id_block->self_id, Toolbox_ObjectDeleted, _SaveDir_deletedhandler, handle)); RE(ViewsMenu_remove(id_block->self_id)); /* free memory */ #ifndef SAVEAS_CRAP free(blk->reset_file_name); #endif free(blk->input_path); free(blk); if(last_savebox == id_block->self_id) last_savebox = NULL_ObjectId; return 1; /* claim event */ }